package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员SERVICE实现类
 */
@DubboService
@Slf4j
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Transactional
    @Override
    public Member createOrUpdate(Member member) {
        log.info("[会员-添加]data:{}",member);
        Member pojo = memberDao.selectByPhoneNumber(member.getPhoneNumber());
        if(null != pojo) {
            //会员已存在，则更新
            member.setId(pojo.getId());
            memberDao.updateByIdSelective(member);
        }else {
            //调用DAO插入数据
            member.setRegTime(new Date());
            memberDao.insert(member);
        }
        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findById(Integer id) {

        return memberDao.selectById(id);
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        List<Integer> resultLIst = new ArrayList<>();
        for (String month : months) {
            String endDate = month+".31";
            Long count = memberDao.countByRegTimeBefore(endDate);
            resultLIst.add(count.intValue());
        }
        return resultLIst;
    }

    @Override
    public List<Map<String,Double>>countByMember() {
        return memberDao.countByMember();
    }

    @Override
    public List<Map<String, Object>> countByAge() throws ParseException {
        List<Map<String, Object>> objects = new ArrayList<>();
        HashMap<String, Object> child = new HashMap<>();
        child.put("value",AgeFunction(0, 18));
        child.put("name","0-18");
        objects.add(child);

        Map<String, Object> young = new HashMap<>();
        young.put("value",AgeFunction(18, 30));
        young.put("name","18-30");
        objects.add(young);

        Map<String, Object> old = new HashMap<>();
        old.put("value",AgeFunction(30, 45));
        old.put("name","30-45");
        objects.add(old);

        Map<String, Object> older = new HashMap<>();
        older.put("value",AgeFunction(45, null));
        older.put("name","45以上");
        objects.add(older);
        return objects;
    }

    public Integer AgeFunction(Integer birthDate,Integer nowDate) throws ParseException {
        //日期格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String monthAndDay = format.substring(4);//-01-14
        String year = format.substring(0, 4);//2021
        Integer count=0;
        Date start=null;
        Date now=null;

        start = simpleDateFormat.parse(Integer.toString(Integer.parseInt(year) - birthDate) + monthAndDay);
        if (nowDate!=null){
            now = simpleDateFormat.parse(Integer.toString(Integer.parseInt(year) - nowDate) + monthAndDay);
            count=memberDao.AgeBandFunction(start,now);
            //否则的话都是45岁以上的
        }else{
            count = memberDao.AgeBandFunctionNull(start);
        }
        return count;
    }
}
